﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using vnFingerPrint.DataAccess;
using vnFingerPrint.BusinessObject;
using vnCommonGUI;
namespace vnFingerPrint.GUI
{
    public partial class frmNewUser : Form
    {
        public Users_Object current;
        string UsersImageUrl;
        public string mode = "add";
        public frmNewUser(string _UserName)
        {
            Users_Object EditObj = Users_DataAccess.GetObject(_UserName);
            current = EditObj;
            InitializeComponent();
            Text = current.UserName;
            txtHoTen.Text = current.FullName;
            txtEmail.Text = current.Email;
            txtSoDienThoai.Text = current.MobilePhone;
            txtPassword.Text = txtPasswordRetype.Text = current.Password;
            txtChucVu.Text = current.ChucVu;
            txtDonViCongTac.Text = current.DonViCongTac;
            txtUserName.Text = current.UserName;
            txtUserName.Enabled = false;
            if (current.Birthday.HasValue) DateNgaySinh.Value = current.Birthday.Value;
            mode = "edit";
            this.Text = "Sửa thông tin của tài khoản đăng nhập: " + current.UserName;
            if (current.ImageUrl != "" && current.ImageUrl != null)
                pbChanDung.Image = new Bitmap(Config_DataAccess.GetUserImagePath(current.ImageUrl));
        }
        public frmNewUser(Users_Object EditObj)
        {
            current = EditObj;
            InitializeComponent();
            Text = current.UserName;
            txtHoTen.Text = current.FullName;
            txtEmail.Text = current.Email;
            txtSoDienThoai.Text = current.MobilePhone;
            txtPassword.Text = txtPasswordRetype.Text = current.Password;
            txtChucVu.Text = current.ChucVu;
            txtDonViCongTac.Text = current.DonViCongTac;
            txtUserName.Text = current.UserName;
            if (current.Birthday.HasValue)
                DateNgaySinh.Value = current.Birthday.Value;
            if (current.ImageUrl != "" && current.ImageUrl != null)
                pbChanDung.Image = new Bitmap(Config_DataAccess.GetUserImagePath(current.ImageUrl));
            txtUserName.Enabled = false;
            mode = "edit";
            this.Text = "Sửa thông tin của tài khoản đăng nhập: " + current.UserName;
        }
        public frmNewUser()
        {
            InitializeComponent();
            this.Text = "Tạo mới tài khoản đăng nhập";
        }
        bool validate()
        {
            if (txtUserName.Text.Trim() == "") return false;
            if (txtPassword.Text != txtPasswordRetype.Text) return false;
            return true;
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!validate()) return;
            Users_Object newObj = new Users_Object();
            newObj.UserName = txtUserName.Text;
            newObj.FullName = txtHoTen.Text;
            newObj.Email = txtEmail.Text;
            newObj.MobilePhone = txtSoDienThoai.Text;
            newObj.Password = txtPassword.Text;
            newObj.Birthday = DateNgaySinh.Value;
            newObj.ChucVu = txtChucVu.Text;
            newObj.PermissionId = int.Parse(cbPermission.SelectedValue.ToString());
            newObj.DonViCongTac = txtDonViCongTac.Text;
            if (pbChanDung.Image == null) newObj.ImageUrl = "";
            else newObj.ImageUrl = newObj.UserName + ".jpg";
            int i;
            if (mode == "add")
                i = Users_DataAccess.Insert(newObj);
            else i = Users_DataAccess.SaveChanged(newObj);
            string url = @UsersImageUrl + @"\" + newObj.ImageUrl;
            pbChanDung.Image.Save(url, System.Drawing.Imaging.ImageFormat.Jpeg);
            //MessageBox.Show(i.ToString());
            this.Close();
        }

        private void btnChonAnh_Click(object sender, EventArgs e)
        {
            pbChanDung.Image = CommonFunction.OpenDialogImageFile();
        }

        private void frmNewUser_Load(object sender, EventArgs e)
        {
            try
            {
                UsersImageUrl = Config_DataAccess.GetObject("UsersImageFolder").ConfigValue;
            }
            catch
            {
                MessageBox.Show("Chưa cấu hình đường dẫn ảnh!");
            }
            List<Permission_Object> ps = Permission_DataAccess.SelectAll();
            cbPermission.DataSource = ps;
            cbPermission.DisplayMember = "Name";
            cbPermission.ValueMember = "Id";
            if (current != null)
                try
                {
                    cbPermission.SelectedValue = current.PermissionId;
                }
                catch { }
        }
    }
}
